/*jslint vars:true plusplus:true*/


function Pit(x, y, sprite) {
    
    "use strict";
    var self = this;
    
    var entity = new Entity(sprite, x, y);
    var collider = new Collider(entity, 30, 1, 0, 10);
    
    
    self.draw = function (context) {
        entity.draw(context);
    };
    
    
    // shift along screen with wrap
    self.shift = function (offset) {
        entity.x += offset;
    };
    
    
    // position getter/setter
    self.getPosition = function () {
        return entity.x;
    };
    
    
    self.setPosition = function (pos) {
        entity.x = pos;
    };
}


function Pits() {
    "use strict";
    var pitSize = 50;
    var background = window.game.background;
    var yPos = background.getGroundLevel();
    var sprite = new Sprite("images/pit.png", function (sprite) {
        sprite.offsetX = -sprite.image.width / 2;
        sprite.offsetY = 0;
    });
    
    var pits = [new Pit(400, yPos, sprite), new Pit(800, yPos, sprite), new Pit(1100, yPos, sprite)];
    var self = this;
    
    self.update = function () {
        var pitIdx;
        var pit;
        
        for (pitIdx = 0; pitIdx < pits.length; pitIdx++) {
            pit = pits[pitIdx];
            
            pit.shift(-background.getBaseSpeed() / window.game.frameRate);
            
            if (pit.getPosition() < -64) {
                pit.setPosition(pit.getPosition() + window.game.canvas.width + 128);
            }
        }
    };
    
    
    self.draw = function (context) {
        var pitIdx;
        var pit;
        
        for (pitIdx = 0; pitIdx < pits.length; pitIdx++) {
            pit = pits[pitIdx];
            pit.draw(context);
        }
    };
}

window.game.addSubsystem(new Pits());